package com.haojie.server;

import com.haojie.register.ServiceRegister;
import com.haojie.register.ZkServiceRegister;

import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.Map;

public class ServiceProvider {

    private Map<String, Object> serviceProvide;
    private ServiceRegister serviceRegister;
    private String host;
    private int port;
    public ServiceProvider(String host, int port) {
        this.host = host;
        this.port = port;
        this.serviceProvide = new HashMap<String, Object>();
        this.serviceRegister = new ZkServiceRegister();
    }

    public void registerService(Object service) {

        String serviceName = service.getClass().getName();

        Class<?>[] interfaceName = service.getClass().getInterfaces();

        for(Class clazz : interfaceName) {
            serviceProvide.put(clazz.getName(), service);
            serviceRegister.register(clazz.getName(), new InetSocketAddress(host, port));
        }

    }

    public Object getService(String interfaceName) {
        return serviceProvide.get(interfaceName);
    }
}
